package ivan.cheng.adapter;

import ivan.cheng.R;
import ivan.cheng.beans.permissionInfo;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MainPermissionListAdapters extends BaseExpandableListAdapter {
	private LayoutInflater mInflater;
	private ArrayList<ArrayList<permissionInfo>> childlist;
	private ArrayList<String> parentLst;
	private Context context;
	public MainPermissionListAdapters(Context context, 
			ArrayList<ArrayList<permissionInfo>> mPermissionList,
			ArrayList<String> parentLst) {
		// Cache the LayoutInflate to avoid asking for a new one each time.
		mInflater = LayoutInflater.from(context);
		this.childlist = mPermissionList;
		this.parentLst = parentLst;
		this.context = context;
		
	}

	private static class ViewHolder {
        ImageView icon;
        TextView text_name;
        TextView text_detail;
        TextView text_count;
    }

	@Override
	public int getGroupCount() 
	{
		return parentLst.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) 
	{
		return childlist.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) 
	{
		return parentLst.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.member_listview, null);
		}

		TextView title = (TextView) view.findViewById(R.id.content_001);
		title.setText(getGroup(groupPosition).toString());

		ImageView image=(ImageView) view.findViewById(R.id.tubiao);
		if(isExpanded)
			image.setBackgroundResource(R.drawable.btn_browser2);
		else image.setBackgroundResource(R.drawable.btn_browser);
			
		return view;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_permission_item, null);

			holder = new ViewHolder();
			holder.icon = (ImageView) convertView.findViewById(R.id.list_icon);
			holder.text_name = (TextView) convertView
					.findViewById(R.id.list_name);
			holder.text_detail = (TextView) convertView
					.findViewById(R.id.permission_detail);
			holder.text_count = (TextView) convertView
					.findViewById(R.id.permission_count);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final permissionInfo pi =childlist.get(groupPosition).get(childPosition);
		convertView.setVisibility(View.VISIBLE);
		// String cmd = dp.getRuninfo().processName;
		holder.icon.setImageResource(pi.nDrawableID);
		holder.text_name.setText(pi.showName);
		holder.text_detail.setText(pi.DetailName);
		holder.text_count.setText(this.context.getString(R.string.application_count, pi.packageNameLst.size()));
		// PsRow row = dp.getPsrow();
		// if (row == null) {
		// holder.text_size.setText(R.string.memory_unknown);
		// } else {
		// holder.text_size.setText((int) Math.ceil(row.mem / 1024) + "K");
		// }

		// convertView.setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View view) {
		// MiscUtil.getTaskMenuDialog(ctx, dp).show();
		// }
		//
		// });

		return convertView;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
}
